//polymorphism 多态

var makesound = function(animal) {
    animal.sound();
}

var Duck = function() {};

Duck.prototype.sound = function() {
    console.log('嘎嘎嘎');
}

var Dog = function() {};

Dog.prototype.sound = function() {
    console.log('汪汪汪');
}

makesound(new Duck());
makesound(new Dog());



var googleMap = {
    show: function() {
        console.log('开始渲染谷歌地图');
    }
}

var baiduMap = {
    show: function() {
        console.log('开始渲染百度地图');
    }
}

var renderMap = function(map) {
    if(map.show instanceof Function) {
        map.show();
    }
}


//apply、call

//1、
document.getElementById = function(func) {
    return function() {
        return func.apply(document, arguments);
    }
}(document.getElementById);

var getId = document.getElementById;

var div = getId('menu');

alert(div.id);

//2、
var func = function(a, b, c) {
    alert([a, b, c]);
}

func.apply(null, [1, 2, 3]);

func.call(null, 1, 2, 3);


//3、
Math.max.apply(null, [1, 2, 3, 4, 5]);

//4、
var obj1 = { 
    name: 'sven'
};
var obj2 = { 
    name: 'anne'
};
window.name = 'window';
var getName = function(){ 
    alert ( this.name );
};

getName(); 
getName.call(obj1);  //'sven'
getName.call(obj2);  //'anne'
getName.call(null, obj2);  //'window'


//closure 闭包

var func = function() {
    a = 1;
}





























